package com.gemframework.modules.extent;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gemframework.model.common.BaseResultData;
import com.gemframework.model.entity.po.Agent;
import com.gemframework.model.entity.po.Count;
import com.gemframework.model.enums.ExceptionCode;
import com.gemframework.service.AgentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping("/Agent")
public class AgentAPI {

    @Resource
    private AgentService agentService;

    //根据id查询代理表信息
    @GetMapping("/selAgentById")
    public BaseResultData selAgentById(Integer id){
        return BaseResultData.SUCCESS(agentService.getById(id));
    }

    //代理表信息添加
    @PostMapping("/insert")
    public BaseResultData insert(@RequestBody Agent agent){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",agent.getPhone());
        queryWrapper.eq("pwd",agent.getPwd());
        queryWrapper.eq("code",agent.getCode());
        Agent one = agentService.getOne(queryWrapper);
        if(one==null){
            return BaseResultData.ERROR(ExceptionCode.DATA_EXIST);
        }
        return BaseResultData.SUCCESS(agentService.save(agent))  ;
    }

    //代理登录
    @PostMapping("/setPassWord")
    public BaseResultData setPassWord(@RequestBody Agent agent){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",agent.getPhone());
        Agent one = agentService.getOne(queryWrapper);
        if(one==null){
            return BaseResultData.ERROR(ExceptionCode.LOGIN_FAIL_INCORRECTCREDENTIALS);
        }
        return BaseResultData.SUCCESS(one);
    }

    //代理新增用户数查询
    @PostMapping("/selAgentId")
    public int selAgentId(@RequestBody Count count){
        return agentService.selAgentId(count.getId(),count.getCreDate());
    }
}
